package com.zhixing.service.executor;

import com.zhixing.model.SwaggerDefinitionModelAttr;
import com.zhixing.service.parse.OptionalSwaggerPathParser;
import com.zhixing.service.parse.SwaggerDefinitionsDefinitionsParser;
import com.zhixing.service.parse.SwaggerJsonStrDefinitionsParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * swagger 2 word （可选择导出部分文档信息）
 * @author zhixing
 */
public class OptionalSwagger2WordExecutor extends AbstractSwagger2WordExecutor {

    /** 参与文档生成的 tags 标签 **/
    private List<String> includeTags;

    public OptionalSwagger2WordExecutor(String swaggerJsonStr,List<String> includeTags) {
        super(swaggerJsonStr);
        this.includeTags = includeTags;
        // 进行 json 处理得到结果集
        executor();
    }

    @Override
    protected void doProcessSwaggerJsonStr() throws IOException {

        // 1、将整串 json 解析成 Map 结构
        Map<String, Object> allMapResult = SwaggerJsonStrDefinitionsParser.createBuild()
                .swaggerJsonStr(this.swaggerJsonStr)
                .build()
                .parse()
                .getAllMapResult();

        //解析model
        Map<String, SwaggerDefinitionModelAttr> definitionMap = SwaggerDefinitionsDefinitionsParser.createBuild()
                .allMap(allMapResult)
                .build()
                .parse()
                .getSwaggerModelAttrMapResult();

        //解析paths
        this.setApiResultList(
                OptionalSwaggerPathParser.createBuild()
                        .allMap(allMapResult)
                        .definitinMap(definitionMap)
                        .includeTags(Optional.ofNullable(includeTags).orElse(new ArrayList<>()))
                        .build()
                        .parse()
                        .getResultTableList()
        );
        this.setAllResultMap(allMapResult);
    }
}
